<?php

namespace app\queue;

use support\Db;
use support\Log;
use Webman\RedisQueue\Consumer;

class ExchangeRatesQueue implements Consumer
{
    /**
     * 队列名称
     *
     * @var string
     */
    public $queue = 'exchange_rates_queue';

    /**
     * 连接名称
     *
     * @var string
     */
    public $connection = 'default';

    /**
     * 消费日志数据
     *
     * @param array $data 日志数据
     */
    public function consume($data)
    {
        $now = date('Y-m-d H:i:s');
        $dataList = [];
        foreach ($data['rates'] as $currencyCode => $rate) {
            $dataList[] = [
                'currency_code' => $currencyCode,
                'rate_to_cny'   => $rate,
                'create_time'   => $now,
                'update_time'   => $now,
            ];
        }
        Db::table('exchange_rates')->upsert(
            $dataList,
            ['currency_code'], // 唯一主键
            ['rate_to_cny', 'update_time']
        );
    }

}